import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
// import componentsRouter from './modules/components'
// import chartsRouter from './modules/charts'
// import tableRouter from './modules/table'
// import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: 'dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'dashboard', icon: 'dashboard', noCache: true, affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/org',
    component: Layout,
    meta: { title: '组织', icon: 'peoples' },
    redirect: '/org/department/list',
    children: [
      {
        path: 'department/list',
        component: () => import('@/views/org/department/list'),
        name: 'Department',
        meta: { title: '部门', icon: 'peoples', noCache: true }
      },
      {
        path: 'employee/list',
        component: () => import('@/views/org/employee/list'),
        name: 'Employee',
        meta: { title: '员工', icon: 'peoples', noCache: true }
      },
      {
        path: 'position/list',
        component: () => import('@/views/org/position/list'),
        name: 'Position',
        meta: { title: '职位', icon: 'peoples', noCache: true }
      },
    ]
  },
  {
    path: '/sys-user',
    component: Layout,
    meta: { title: '账号', icon: 'excel' },
    redirect: '/sys-user/profile/list',
    children: [
      {
        path: 'profile/list',
        component: () => import('@/views/sys-user/profile/list'),
        name: 'Profile',
        meta: { title: '账号列表', icon: 'excel', noCache: true }
      },
      {
        path: 'login-log',
        component: () => import('@/views/sys-user/login-log/list'),
        name: 'LoginLog',
        meta: { title: '登录日志', icon: 'documentation', noCache: true }
      }
    ]
  },
  {
    path: '/app',
    component: Layout,
    meta: { title: '应用', icon: 'tree-table' },
    redirect: '/app/app/list',
    children: [
      {
        path: 'app/list',
        component: () => import('@/views/app/app/list'),
        name: 'App',
        meta: { title: '应用', icon: 'table' },
      },
      {
        path: 'nav-bar/list',
        component: () => import('@/views/app/nav-bar/list'),
        name: 'NavBar',
        meta: { title: '导航', icon: 'table' },
      },
      {
        path: 'nav-menu/list',
        component: () => import('@/views/app/nav-menu/list'),
        name: 'NavMenu',
        meta: { title: '菜单', icon: 'table' },
      },
    ]
  },
  {
    path: '/content',
    component: Layout,
    meta: { title: '内容', icon: 'tree-table' },
    redirect: '/content/column/list',
    children: [
      {
        path: 'column/list',
        component: () => import('@/views/content/column/list'),
        name: 'Column',
        meta: { title: '栏目', icon: 'table', noCache: true }
      },
      {
        path: 'image/list',
        component: () => import('@/views/content/image/list'),
        name: 'Image',
        meta: { title: '图片', icon: 'example', noCache: true }
      },
      {
        path: 'rich/list',
        component: () => import('@/views/content/rich/list'),
        name: 'Rich',
        meta: { title: '图文', icon: 'education', noCache: true }
      },
      {
        path: 'video/list',
        component: () => import('@/views/content/video/list'),
        name: 'Video',
        meta: { title: '视频', icon: 'guide', noCache: true }
      },
      {
        path: 'tag/list',
        component: () => import('@/views/content/tag/list'),
        name: 'Tag',
        meta: { title: '标签', icon: 'star', noCache: true }
      }
    ]
  },
  {
    path: '/product',
    component: Layout,
    meta: { title: '产品', icon: 'table' },
    redirect: '/product/product/list',
    children: [
      {
        path: 'category/list',
        component: () => import('@/views/product/category/list'),
        name: 'ProductCategory',
        meta: { title: '产品分类', icon: 'nested', noCache: true }
      },
      {
        path: 'spec/list',
        component: () => import('@/views/product/spec/list'),
        name: 'Spec',
        meta: { title: '产品规格', icon: 'nested', noCache: true }
      },
      {
        path: 'spu/list',
        component: () => import('@/views/product/spu/list'),
        name: 'Spu',
        meta: { title: '产品模型', icon: 'example', noCache: true }
      },
      {
        path: 'product/list',
        component: () => import('@/views/product/product/list'),
        name: 'Product',
        meta: { title: '产品列表', icon: 'list', noCache: true }
      },
      {
        path: 'product/edit',
        component: () => import('@/views/product/product/edit'),
        meta: { title: '编辑产品', icon: 'table', noCache: true },
        hidden: true
      },
    ]
  },
  {
    path: '/service',
    component: Layout,
    meta: { title: '服务', icon: 'tree-table' },
    redirect: '/service/category/list',
    children: [
      {
        path: 'category/list',
        component: () => import('@/views/service/category/list'),
        name: 'ServiceCategory',
        meta: { title: '服务分类', icon: 'table' }
      },
      {
        path: 'level/list',
        component: () => import('@/views/service/level/list'),
        name: 'ServiceLevel',
        meta: { title: '服务级别', icon: 'table' }
      },
      {
        path: 'service/list',
        component: () => import('@/views/service/service/list'),
        name: 'Service',
        meta: { title: '服务列表', icon: 'table' }
      },
      {
        path: 'price-detail/list',
        component: () => import('@/views/service/price-detail/list'),
        name: 'PriceDetail',
        meta: { title: '区域定价明细', icon: 'component' }
      },
      {
        path: 'price-strategy/list',
        component: () => import('@/views/service/price-strategy/list'),
        name: 'PriceStrategy',
        meta: { title: '城市定价策略', icon: 'component' }
      },
    ]
  },
  {
    path: '/merchant',
    component: Layout,
    meta: { title: '商家', icon: 'tree-table' },
    redirect: '/merchant/merchant/list',
    children: [
      {
        path: 'merchant/list',
        component: () => import('@/views/merchant/merchant/list'),
        name: 'Merchant',
        meta: { title: '商家列表', icon: 'table' },
      },
      {
        path: 'type/list',
        component: () => import('@/views/merchant/type/list'),
        name: 'Type',
        meta: { title: '商家类型', icon: 'table' }
      },
      {
        path: 'apply/list',
        component: () => import('@/views/merchant/apply/list'),
        name: 'Apply',
        meta: { title: '加盟申请', icon: 'table' }
      },
    ]
  },
  {
    path: '/indent',
    component: Layout,
    meta: { title: '订单', icon: 'tree-table' },
    redirect: '/indent/indent/list',
    children: [
      {
        path: 'indent/list',
        component: () => import('@/views/indent/indent/list'),
        name: 'Indent',
        meta: { title: '订单列表', icon: 'skill', noCache: true }
      },
      {
        path: 'evaluation/list',
        component: () => import('@/views/indent/evaluation/list'),
        name: 'Evaluation',
        meta: { title: '订单评价', icon: 'skill', noCache: true }
      },
      {
        path: 'after-sale/list',
        component: () => import('@/views/indent/after-sale/list'),
        name: 'AfterSale',
        meta: { title: '售后服务', icon: 'skill', noCache: true }
      },
    ]
  },
  {
    path: '/reservation',
    component: Layout,
    meta: { title: '预约', icon: 'tree-table' },
    redirect: '/reservation/reservation/list',
    children: [
      {
        path: 'reservation/list',
        component: () => import('@/views/reservation/reservation/list'),
        name: 'Reservation',
        meta: { title: '预约服务', icon: 'table' }
      },
    ]
  },
  {
    path: '/finance',
    component: Layout,
    meta: { title: '财务', icon: 'tree-table' },
    redirect: '/finance/finance/list',
    children: [
      {
        path: 'finance/list',
        component: () => import('@/views/finance/finance/list'),
        name: 'Finance',
        meta: { title: '财务', icon: 'table' }
      }
    ]
  },
  {
    path: '/base',
    component: Layout,
    meta: { title: '系统', icon: 'tree-table' },
    redirect: '/base/config/list',
    children: [
      {
        path: 'config/list',
        component: () => import('@/views/base/config/list'),
        name: 'Config',
        meta: { title: '配置数据', icon: 'component' }
      },
      {
        path: 'dictionary/list',
        component: () => import('@/views/base/dictionary/list'),
        name: 'Dictionary',
        meta: { title: '字典数据', icon: 'table' },
      },
      {
        path: '/district',
        component: () => import('@/views/district/district'),
        meta: { title: '行政区划', icon: 'component' },
        redirect: '/district/district/list',
        children: [
          {
            path: 'level/list',
            component: () => import('@/views/district/level/list'),
            name: 'Level',
            meta: { title: '区域级别', icon: 'component' }
          },
          {
            path: 'district/list',
            component: () => import('@/views/district/district/list'),
            name: 'District',
            meta: { title: '行政区域', icon: 'component' }
          },
          {
            path: 'level-detail/list',
            component: () => import('@/views/district/level-detail/list'),
            name: 'LevelDetail',
            meta: { title: '城市分级明细', icon: 'component' }
          },
        ]
      },
      {
        path: '/permission',
        component: () => import('@/views/base/permission/permission'),
        meta: { title: '权限', icon: 'component' },
        redirect: '/permission/sys-role/list',
        children: [
          {
            path: 'sys-role/list',
            component: () => import('@/views/permission/sys-role/list'),
            name: 'SysRole',
            meta: { title: '角色管理', icon: 'component' }
          },
          {
            path: 'sys-interface/list',
            component: () => import('@/views/permission/sys-interface/list'),
            name: 'SysInterface',
            meta: { title: '接口管理', icon: 'component' }
          },
        ],
      },
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  base:'/admin/',  //
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
